package MTT::Controller::Setting;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

MTT::Controller::Setting - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

use POSIX qw(strftime);
use MTT::Form::Switches;
use MTT::Form::Fields;
use MTT::Libs::Menu;
use utf8;

sub switches :Path('/setting/switches') :Args(0) {
    my ( $self, $c ) = @_;

	my $form = MTT::Form::Switches->new(schema => $c->model('DBIC'));

	if ( $c->req->method eq 'POST' ) {
		if ( $form->process(params => $c->req->params ) ) {

			if ( $c->req->params->{type_save} ) {

				if ( $c->req->params->{types} ) {
					if ( my $type = $c->model('DBIC::SwitchType')->find( $c->req->param('id_type') ) ) {
						$type->update({
								type => $form->field('type')->value,
								table_name => $form->field('table_name')->value,
								status => $form->field('type_enabled')->value
							});

						$c->model('DBIC::Switch')->search({id_type => $type->id})->update({status => $form->field('type_enabled')->value});
					}
				}
			}
			elsif ( $c->req->params->{switch_save} ) {

				if ( $c->req->params->{host_code} ) {
					if ( my $switch = $c->model('DBIC::Switch')->find( $c->req->param('id_switch') ) ) {
						$switch->update({
								host_code => $form->field('host_code')->value,
								descr => $form->field('switch_descr')->value,
								id_type => $form->field('switch_type')->value,
								updated => strftime('%Y-%m-%d %H:%M:%S', localtime),
								status => $form->field('switch_enabled')->value
							});
					}
				}
			}

			MTT::Libs::Menu->instance->load_menu($c);
		}
	}

	my @types = $c->model('DBIC::SwitchType')->all();
	my @switches = $c->model('DBIC::Switch')->all();

	$c->stash(
		template => 'switches.tt2',
		form => $form,
		types => \@types,
		switches => \@switches
	);
}


sub get_switches : Path('/setting/switches_by_type') {
	my ($self, $c) = @_;

	my $id_type = $c->req->params->{id_type};

	my @switches;
	foreach ( $c->model('DBIC::Switch')->search({id_type => $id_type}) ) {
		push @switches, { id => $_->id, name => $_->host_code . ' (' . $_->descr . ')' };
	}

	$c->stash->{json_data}->{switches} = \@switches;
	$c->forward('View::JSON');
}


sub fields : Path('/setting/fields') {
	my ($self, $c) = @_;

	my $form = MTT::Form::Fields->new(schema => $c->model('DBIC'));

	if ( $c->req->method eq 'POST' ) {
		if ( $c->req->params->{save} ) {
			if ( $c->req->params->{types} ) {
				$c->model('DBIC::FieldName')->search({
						type => $c->req->params->{type}, 
						field => $c->req->params->{field}
					})->update({
							field => $c->req->params->{field},
							name  => $c->req->params->{name},
							short_name => $c->req->params->{short_name}
						});
			}
			else {
				$c->model('DBIC::FieldName')->create({
						type  => $c->req->params->{type},
						field => $c->req->params->{field},
						name  => $c->req->params->{name},
						short_name => $c->req->params->{short_name}
					});
			}
		}
		elsif ( $c->req->params->{delete} ) {
			$c->model('DBIC::FieldName')->search({
					type => $c->req->params->{type}, 
					field => $c->req->params->{field}
				})->delete();
		}
	}
	my %fields;
	foreach ( $c->model('DBIC::FieldName')->all() ) {
		$fields{ $_->type } = [] unless $fields{ $_->type };
		push @{ $fields{ $_->type } }, [ $_->field, $_->name, $_->short_name ];
	}


	$c->stash(
		template => 'fields.tt2',
		form => $form,
		fields => \%fields
	);
}

=head1 AUTHOR

A clever guy

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut


__PACKAGE__->meta->make_immutable;

1;
